
package com.probiz.estore.catalog.service.impl;

import java.util.List;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.catalog.service.ProductCategoryNtManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.service.ProductManager;

public class ProductCategoryNtManagerImpl implements ProductCategoryNtManager {
	private ProductCategoryManager	productCategoryManager;

	private ProductManager			productManager;

	public void setProductCategoryManager(
			ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}


	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.catalog.service.ProductCategoryNtManager#calItemCountForScheduling()
	 */
	public void calItemCountForScheduling() {
		String hql = "from ProductCategory pc";
		boolean flag = true;
		int pageSize = 10;
		int pageNo = 1;
		do {
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql, null, pageNo, pageSize, null);
			List<ProductCategory> productCategoryList = productCategoryManager.searchByCriteria(sc);
			for (ProductCategory productCategory : productCategoryList) {
				try{
					Integer itemCount = productManager.countProductIncludeAllByCategory(productCategory);
					Integer activeItemCount = productManager.countActiveProductIncludeAllByCategory(productCategory);
					productCategory.setItemCount(itemCount);
					productCategory.setActiveItemCount(activeItemCount);
					productCategoryManager.save(productCategory);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if (productCategoryList.size() != pageSize) {
				flag = false;
			} else {
				++pageNo;
			}
		} while (flag);
	}

}
